package github.kawaiior.botapiserver.controller;

import github.kawaiior.botapiserver.common.ActionResponse;
import github.kawaiior.botapiserver.service.UserService;
import github.kawaiior.botapiserver.util.CaptchaUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class LoginController {

    private final UserService userService;

    public LoginController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/code")
    public ActionResponse getLoginCode(HttpSession session){
        return userService.getLoginCode(session);
    }

    private static final ActionResponse CAPTCHA_ERROR = new ActionResponse(0, "验证码错误");

    @RequestMapping("/login")
    public ActionResponse adminLogin(String account, String password, String code, HttpSession session, HttpServletResponse response){
        // 检查验证码
        if (CaptchaUtil.codeError(code, session)){
            return CAPTCHA_ERROR;
        }
        return userService.adminLogin(account, password, response);
    }

}
